/*
提交链接:https://leetcode.cn/problems/maximum-subarray/description/
53. 最大子数组和
赖德檀 2024/12/13
*/

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int ans=INT_MIN;
        int sum=0;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]+sum<0)
            {
                sum=0;
                ans=max(ans,nums[i]);
            }
            else
            {
                sum+=nums[i];
                ans=max(ans,sum);
            }
        }
        return ans;
    }
};